﻿using UnityEngine;
using System.Collections.Generic;
using static CyberU;
using static CyberU.UI;

public class BladeGesture : MonoBehaviour
{
    private void CheckForCut()
    {
        if (edgeCollider.points.Length < 2)
            return;
        Vector2 start = edgeCollider.points[^2];
        Vector2 end = edgeCollider.points[^1];
        Vector2 worldStart = transform.TransformPoint(start);
        Vector2 worldEnd = transform.TransformPoint(end);
        Debug.DrawLine(worldStart, worldEnd, Color.blue, 1f);
        RaycastHit2D[] hits = Physics2D.LinecastAll(worldStart, worldEnd);
        foreach (RaycastHit2D hit in hits)
        {
            Delog(hit);
            Collider2D col = hit.collider;
            if (col == edgeCollider || col is EdgeCollider2D)
                continue;
            GraphEdge graphEdge = col.GetComponent<GraphEdge>();
            if (graphEdge != null)
            {
                CutGraphEdge(graphEdge);
            }
        }
    }

    public LineRenderer bladeTrail;
    public float minCuttingVelocity = 0.01f;

    public float zOffset
    {
        get { return currentCanvas.transform.localPosition.z; }
    }

    private bool isCutting = false;
    private Vector3 previousPosition;
    private Rigidbody2D bladeRigidbody;
    private EdgeCollider2D edgeCollider;
    public GameObject BladeGameObject;

    void Start()
    {
        BladeGameObject = CreateGameObject(parent: gameObject, name: "blade", position: new Vector3(10000, 10000, zOffset));
        bladeTrail = GetOrAddComponent<LineRenderer>(BladeGameObject);
        bladeTrail.material = new Material(Shader.Find("Unlit/Color"));
        bladeTrail.material.color = Color.red;
        bladeTrail.positionCount = 0;
        bladeTrail.enabled = false;
        bladeTrail.startWidth = 0.05f;
        bladeTrail.endWidth = 0.05f;
        bladeRigidbody = GetOrAddComponent<Rigidbody2D>(BladeGameObject);
        edgeCollider = GetOrAddComponent<EdgeCollider2D>(BladeGameObject);
        bladeRigidbody.isKinematic = true;
        edgeCollider.isTrigger = true;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            StartCutting();
        }
        else if (Input.GetMouseButtonUp(1))
        {
            StopCutting();
        }

        if (isCutting)
        {
            UpdateCut();
        }
    }

    void StartCutting()
    {
        isCutting = true;
        previousPosition = mainCamera.ScreenToWorldPoint(
            new Vector3(Input.mousePosition.x, Input.mousePosition.y, zOffset));

        bladeTrail.enabled = true;
        bladeTrail.positionCount = 1;
        bladeTrail.SetPosition(0, previousPosition);

        edgeCollider.points = new Vector2[] { previousPosition };
    }

    void StopCutting()
    {
        isCutting = false;
        // bladeTrail.enabled = false;
        // bladeTrail.positionCount = 0;
        // edgeCollider.points = new Vector2[0];
    }

    void UpdateCut()
    {
        Vector3 newPosition = mainCamera.ScreenToWorldPoint(
            new Vector3(Input.mousePosition.x, Input.mousePosition.y, zOffset));
        if (Vector3.Distance(newPosition, previousPosition) < minCuttingVelocity)
        {
            return;
        }
        bladeTrail.positionCount++;
        bladeTrail.SetPosition(bladeTrail.positionCount - 1, newPosition);
        previousPosition = newPosition;
        Vector2[] points = new Vector2[bladeTrail.positionCount];
        for (int i = 0; i < bladeTrail.positionCount; i++)
            points[i] = bladeTrail.GetPosition(i);
        edgeCollider.points = points;
        CheckForCut();
    }

    public void CutGraphEdge(GraphEdge edge)
    {
        Debug.Log("EdgeCollider被刀刃切中！");
    }
}
